package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.ProvinceAdapter;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.base.rebaseadapter.MyHolder;
import com.wqtz.chechai.dao.HisCity;
import com.wqtz.chechai.dao.Province;
import com.wqtz.chechai.dao.hiscity.HisCityDao;
import com.wqtz.chechai.dao.province.DaoMaster;
import com.wqtz.chechai.dao.province.DaoSession;
import com.wqtz.chechai.dao.province.ProvinceDao;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/23 10:43
 */

public class NewAddStepTwoCarState1 extends BaseActivity implements ProvinceAdapter.OnItemLickListener {
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.tv_title)
	TextView mTvTitle;
	@BindView(R.id.tv_hiscar)
	TextView mTvHiscar;
	@BindView(R.id.tv_province)
	TextView mTvProvince;
	@BindView(R.id.rv_province)
	RecyclerView mRvProvince;
	@BindView(R.id.btn_1)
	Button mBtn1;
	@BindView(R.id.btn_2)
	Button mBtn2;
	@BindView(R.id.btn_3)
	Button mBtn3;
	@BindView(R.id.btn_4)
	Button mBtn4;
	@BindView(R.id.btn_5)
	Button mBtn5;
	@BindView(R.id.btn_6)
	Button mBtn6;
	@BindView(R.id.btn_7)
	Button mBtn7;
	@BindView(R.id.btn_8)
	Button mBtn8;
	@BindView(R.id.ll_his)
	LinearLayout mLlHis;
	private Province province;
	List<Province> userList;
	List<HisCity> historyList;
	List<HisCity> querys = new ArrayList<>();
	ProvinceAdapter mProvinceAdapter;
	List<Button> mbtns = new ArrayList<>();
	DaoMaster.DevOpenHelper devOpenHelper;
	DaoMaster daoMaster;
	DaoSession daoSession;
	ProvinceDao provinceDao;
	com.wqtz.chechai.dao.hiscity.DaoMaster.DevOpenHelper devOpenHelper1;
	com.wqtz.chechai.dao.hiscity.DaoMaster daoMaster1;
	com.wqtz.chechai.dao.hiscity.DaoSession daoSession1;
	HisCityDao hisCityDao;


	@Override
	public void init() {
		ButterKnife.bind(this);
		devOpenHelper = new DaoMaster.DevOpenHelper(this, "Province-db", null);
		daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
		daoSession = daoMaster.newSession();
		provinceDao = daoSession.getProvinceDao();
		devOpenHelper1 = new com.wqtz.chechai.dao.hiscity.DaoMaster.DevOpenHelper(this, "hisCity", null);
		daoMaster1 = new com.wqtz.chechai.dao.hiscity.DaoMaster(devOpenHelper1.getWritableDatabase());
		daoSession1 = daoMaster1.newSession();
		hisCityDao = daoSession1.getHisCityDao();
		userList = provinceDao.queryBuilder()
				//.where(UserDao.Properties.Id.notEq(999))
				.orderAsc(ProvinceDao.Properties.Id)
				.limit(50)
				.build().list();

		Logger.e("查询到的数据" + userList.size() + " " + userList.toString());
		historyList = hisCityDao.queryBuilder()
				//.where(ProvinceDao.Properties.IsSelected.notEq(0))
				.orderAsc(HisCityDao.Properties.CityName)
				.limit(8)
				.build().list();
		Logger.e(historyList.size() + "");
		mProvinceAdapter = new ProvinceAdapter(R.layout.item_province, userList);
		mRvProvince.setLayoutManager(new GridLayoutManager(this, 1));
		mRvProvince.addItemDecoration(new MyItemDecoration());
		mRvProvince.setAdapter(mProvinceAdapter);
		mProvinceAdapter.setOnItemClickListen(this);
		addHisBtn();


	}

	private void addHisBtn() {
		mbtns.add(mBtn1);
		mbtns.add(mBtn2);
		mbtns.add(mBtn3);
		mbtns.add(mBtn4);
		mbtns.add(mBtn5);
		mbtns.add(mBtn6);
		mbtns.add(mBtn7);
		mbtns.add(mBtn8);
		for (int i = 0; i < historyList.size(); i++) {
			mbtns.get(i).setText("" + historyList.get(i).getCityName());
		}
		if (historyList.size() < 8) {
			for (int i = 0; i < 8 - historyList.size(); i++) {
				mbtns.get(historyList.size() + i).setVisibility(View.INVISIBLE);
			}
		}



	}

	private void gethisCity(Button button) {
		querys.clear();
		QueryBuilder<HisCity> qb = hisCityDao.queryBuilder();
		qb.where(HisCityDao.Properties.CityName.eq(button.getText().toString())).orderAsc(HisCityDao.Properties.CityName);
		querys = qb.list();
	}


	@Override
	public void loadData() {


	}

	@Override
	public int setContentViews() {
		return R.layout.activity_newcarstate1;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}


	@OnClick(R.id.iv_back)
	public void onClick() {
		finish();
	}

	MyHolder mHolder;

	@Override
	public void onItemLick(int position, MyHolder holder) {
	//	ToastUtils.show(this, userList.get(position).getProName(), 2000);
		mHolder = holder;
		Intent intent = new Intent(this, NewAddStepTwoCarState2.class);
		intent.putExtra("proID", userList.get(position).getProID());
		intent.putExtra("provinceName", userList.get(position).getProName());
		startActivityForResult(intent, 201);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 201 && resultCode == RESULT_OK) {
			Logger.e(data.getStringExtra("city_name"));
			Intent intent = new Intent();
			intent.putExtra("city_name", data.getStringExtra("city_name"));
			intent.putExtra("city_id", data.getStringExtra("city_id"));
			intent.putExtra("pro_id", data.getStringExtra("pro_id"));
			setResult(RESULT_OK, intent);
			this.finish();
		}
	}

	@OnClick({R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8})
	public void onClick(View view) {
		onItemLick((Button) view);
	}


	public void onItemLick(Button btn) {
		Intent intent = new Intent();
		gethisCity(btn);
		if(querys.size()!=0){
			intent.putExtra("city_name", querys.get(0).getCityName());
			intent.putExtra("city_id", querys.get(0).getCityID());
			intent.putExtra("pro_id", querys.get(0).getProID());
			setResult(RESULT_OK, intent);
		}

		this.finish();
	}


	class MyItemDecoration extends RecyclerView.ItemDecoration {
		/**
		 * @param outRect 边界
		 * @param view    recyclerView ItemView
		 * @param parent  recyclerView
		 * @param state   recycler 内部数据管理
		 */
		@Override
		public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
			//设定底部边距为1px
			outRect.set(0, 0, 0, 1);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
	}
}
